/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms; 

namespace Shuriken.Core.Store
{
    public class ImageStore
    {
        private static ImageList m_ImageList; 
        public static int DefaultImageWidth = 32; 
        public static int DefaultImageHeight = 32;

        private static string DefaultActionImage = string.Empty;
        private static string DefaultSubjectImage = string.Empty;

        /// <summary>
        /// Returns the default instance 
        /// </summary>
        /// <returns>default instance</returns>
        private static ImageList GetDefaultInstance()
        {
            if (m_ImageList == null)
            {
                m_ImageList = new ImageList();
                m_ImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
                m_ImageList.ImageSize = new System.Drawing.Size(32, 32);
                m_ImageList.TransparentColor = System.Drawing.Color.Transparent;
            }

            return m_ImageList; 
        }

        /// <summary>
        /// Adds a system image list index to the store 
        /// </summary>
        /// <param name="index">index to be added </param>
        /// <returns>key to this resource</returns>
        public static string AddSystemImageListIndex(int index)
        {
            return "index.systemimagelistindex." + index; 
        }

        /// <summary>
        /// Adds a resource image 
        /// </summary>
        /// <param name="icon">icon to add</param>
        /// <returns>key to this image</returns>
        public static string AddResourceImage(System.Drawing.Icon icon)
        {
            System.Drawing.Bitmap bmp = icon.ToBitmap();
            return ImageStore.AddResourceImage((System.Drawing.Image)bmp);
        }

        /// <summary>
        /// Adds a resource image 
        /// </summary>
        /// <param name="icon">image to add</param>
        /// <returns>key to this image</returns>
        public static string AddResourceImage(System.Drawing.Image image)
        {
            System.Diagnostics.Debug.Assert(image != null, "ImageStore::AddResource received a null image!");

            int index = ImageStore.GetDefaultInstance().Images.Count;
            ImageStore.GetDefaultInstance().Images.Add(image);

            return "index.resourceimageindex." + index; 
        }

        /// <summary>
        /// Draws an image based on the key 
        /// </summary>
        /// <param name="g">graphic context</param>
        /// <param name="key">key for the image</param>
        public static void DrawImage(System.Drawing.Graphics g, string key)
        {
            DrawImage(g, 0, 0, key); 
        }

        /// <summary>
        /// Draws an image based on the key 
        /// </summary>
        /// <param name="g">graphic context</param>
        /// <param name="width">width</param>
        /// <param name="height">height</param>
        /// <param name="key">key for the image</param>
        public static void DrawImage(System.Drawing.Graphics g, int x, int y, string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                DrawImage(g, x, y, DefaultImageWidth, DefaultImageHeight, key);
            }
        }

        /// <summary>
        /// Draws an image based on the key 
        /// </summary>
        /// <param name="g">graphic context</param>
        /// <param name="x">x coordinate</param>
        /// <param name="y">y cooordinate</param>
        /// <param name="width">width</param>
        /// <param name="height">height</param>
        /// <param name="key">key for the image</param>
        public static void DrawImage(System.Drawing.Graphics g, int x, int y, int width, int height, string key)
        {
            // broke up the key and draw according that
            string[] key_parts = key.Split(new char[] { '.' });

            // check integrity of the key 
            System.Diagnostics.Debug.Assert(key_parts.Length == 3, "ImageStore::DrawImage received an invalid key!");
            System.Diagnostics.Debug.Assert(key_parts[0].Equals("index"), "ImageStore::DrawImage received an invalid key!");

            int index = 0; 
            bool result = false; 

            switch (key_parts[1])
            {
                case "resourceimageindex":
                    result = int.TryParse(key_parts[2], out index);

                    System.Diagnostics.Debug.Assert(result, "ImageStore::DrawImage received an invalid key!");
                    DrawImageFromImageStore(g, x, y, width, height, index); 
                    break;

                case "systemimagelistindex":
                    result = int.TryParse(key_parts[2], out index);
                    System.Diagnostics.Debug.Assert(result, "ImageStore::DrawImage received an invalid key!");

                    DrawImageFromSystemImageList(g, x, y, width, height, index); 
                    break;

                default:
                        System.Diagnostics.Debug.Assert(false, "ImageStore::DrawImage received an invalid key!");
                    break;
            }
        }

        /// <summary>
        /// Draws an image from the image store 
        /// </summary>
        /// <param name="g">graphic context</param>
        /// <param name="x">x coordinate</param>
        /// <param name="y">y cooordinate</param>
        /// <param name="width">width</param>
        /// <param name="height">height</param>
        /// <param name="index">index of the image in the image store</param>
        private static void DrawImageFromImageStore(System.Drawing.Graphics g, int x, int y, int width, int height, int index)
        {
            System.Drawing.Image img = m_ImageList.Images[index];
            g.DrawImage(img, x, y); 
        }

        /// <summary>
        /// Draws an image from the system image list 
        /// </summary>
        /// <param name="g">graphic context</param>
        /// <param name="x">x coordinate</param>
        /// <param name="y">y cooordinate</param>
        /// <param name="width">width</param>
        /// <param name="height">height</param>
        /// <param name="index">index of the image in the system image list</param>
        private static void DrawImageFromSystemImageList(System.Drawing.Graphics g, int x, int y, int width, int height, int index)
        {
            Shell.ShellManager.Instance.DrawShellImage(g, x, y, width, height, index); 
        }

        internal static string GetDefaultActionImage()
        {
            if (string.IsNullOrEmpty(DefaultActionImage))
            {
                System.Drawing.Icon defaultIcon = Shuriken.Core.Properties.Resources.ArtifactsIcon;
                System.Diagnostics.Trace.Assert(defaultIcon != null, "Failed to load default icon for action.");
                DefaultActionImage = ImageStore.AddResourceImage(defaultIcon);
            }

            return DefaultActionImage;
        }

        internal static string GetDefaultSubjectImage()
        {
            if (string.IsNullOrEmpty(DefaultSubjectImage))
            {
                System.Drawing.Icon defaultIcon = Shuriken.Core.Properties.Resources.DefaultApp;
                System.Diagnostics.Trace.Assert(defaultIcon != null, "Failed to load default icon for action.");
                DefaultSubjectImage = ImageStore.AddResourceImage(defaultIcon);

            }

            return DefaultSubjectImage;
        }
    }

}
